package com.graphqljava.demo.gql.gql.fetchers;

import com.graphqljava.demo.gql.entity.AchieveInfo;
import com.graphqljava.demo.gql.entity.PersonInfo;
import com.graphqljava.demo.gql.service.AchieveInfoService;
import com.graphqljava.demo.gql.service.PersonInfoService;
import graphql.schema.DataFetcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * com.graphqljava.demo.gql
 *
 * @author Oscar.Chen
 * @date 6/15/2020 12:36 PM
 */
@Component
public class GlxyGraphQLDataFetchers {

    @Autowired
    AchieveInfoService achieveInfoService;

    @Autowired
    PersonInfoService personInfoService;

    public DataFetcher getAchieveByIdDataFetcher() {
        return dataFetchingEnvironment -> {
            String id = dataFetchingEnvironment.getArgument("id");
            return achieveInfoService.findAchieveInfoById(id).orElse(null);
        };
    }


    public DataFetcher getPersonByIdDataFetcher() {
        return dataFetchingEnvironment -> {
            String id = dataFetchingEnvironment.getArgument("id");
            return personInfoService.findPersonInfoById(id).orElse(null);
        };
    }

    public DataFetcher getAllAchieveDataFetcher() {
        return dataFetchingEnvironment -> achieveInfoService.allAchieveInfos();
    }

    public DataFetcher getAllPersonDataFetcher() {
        return dataFetchingEnvironment -> personInfoService.allPersonInfos();
    }

    public DataFetcher getPersonByPersonIdDataFetcher() {
        return dataFetchingEnvironment -> {
            String id = dataFetchingEnvironment.getArgument("id");
            return personInfoService.allPersonInfosByPersonId(id);
        };
    }

    public DataFetcher getPersonByProjectIdDataFetcher() {
        return dataFetchingEnvironment -> {
            String id = dataFetchingEnvironment.getArgument("id");
            return personInfoService.allPersonInfosByProjectId(id);
        };
    }

    public DataFetcher getAchieveDataFetcher() {
        return dataFetchingEnvironment -> {
            PersonInfo person = dataFetchingEnvironment.getSource();
            return achieveInfoService.findAchieveInfoById(person.getProjectId());
        };
    }

    public DataFetcher getPersonDataFetcher() {
        return dataFetchingEnvironment -> {
            AchieveInfo achieve = dataFetchingEnvironment.getSource();
            return personInfoService.allPersonInfosByProjectId(achieve.getId());
        };
    }


//    public DataFetcher getUserByIdDataFetcher() {
//        return dataFetchingEnvironment -> {
//            String userId = dataFetchingEnvironment.getArgument("id");
//            return users
//                .stream()
//                .filter(e -> e.get("id").equals(userId))
//                .findFirst()
//                .orElse(null);
//        };
//    }
//
//    public DataFetcher getUserDataFetcher() {
//        return dataFetchingEnvironment -> {
//            Map<String, String> user = dataFetchingEnvironment.getSource();
//            String userId = user.get("userId");
//
//            return users
//                .stream()
//                .filter(e -> e.get("id").equals(userId))
//                .findFirst()
//                .orElse(null);
//        };
//    }
//
//    public DataFetcher getPostDataFetcher() {
//        return dataFetchingEnvironment -> {
//            Map<String, String> source = dataFetchingEnvironment.getSource();
//            String userId = source.get("id");
//
//            Object[] items = posts
//                .stream()
//                .filter(e -> e.get("userId").equals(userId))
//                .toArray();
//            if (items.length > 0) {
//                return items;
//            } else {
//                return null;
//            }
//        };
//    }

}
